using Gateway.Domain.Entities;
using Gateway.IApplication.RequestLog;
using Gateway.IApplication.RequestLog.Input;
using Gateway.IRepositories;
using Microsoft.Extensions.DependencyInjection;

namespace Gateway.Application;
/// <summary>
/// 
/// </summary>
public class RequestLogApp : IRequestLogApp
{
    private readonly IServiceScopeFactory _scopeFactory;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="scopeFactory"></param>
    public RequestLogApp(IServiceScopeFactory scopeFactory)
    {
        _scopeFactory = scopeFactory;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Add(AddRequestLogInput input)
    {
        using (var scope = _scopeFactory.CreateScope())
        {
            var repository = scope.ServiceProvider.GetService<IAppRepository<Domain.Entities.RequestLog>>();
            var unitOfWork = scope.ServiceProvider.GetService<IUnitOfWork>();
            var entity = new RequestLog(logType: input.LogType, logName: input.LogName, apiPath: input.ApiPath,
                httpMethod: input.HttpMethod, ip: input.Ip, @event: input.Event,
                creatorName: input.CreatorName, createdTime: input.CreatedTime, creatorBy: input.CreatedBy);
            await repository.AddAsync(entity);
            await unitOfWork.CommitAsync();
        }
    }
}